﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Battle;
using Item.Buff;
using LitJson;

namespace ItemComponent
{
    /// <summary>
    /// 炮
    /// </summary>
    public class Cannon : Gun //, IBulletHitHandler
    {
        protected const string Key_RangeData = "RangeData";

#if UNITY_EDITOR
        [SerializeField,ReadOnly]
        private ST_RangeData debugRangeData;
#endif

        public ST_RangeData RangeData { get; protected set; }
        public ST_RangeData CurRangeData { get; protected set; }

        public override void InitComponent(JsonData data, Item.ItemBase item)
        {
            if (data == null || data[Key_RangeData] == null) return;

            ST_RangeData tmp = JsonMapper.ToObject<ST_RangeData>(data[Key_RangeData].ToJson());

            RangeData = tmp;
            CurRangeData = tmp;
#if UNITY_EDITOR
            debugRangeData = tmp;
#endif
            base.InitComponent(data,item);
        }

        public override void OnBuffChange(STBuffEffectData buffEffectData)
        {
            base.OnBuffChange(buffEffectData);

            ST_RangeData explodeData = RangeData;

            explodeData.dRadius *= buffEffectData.fRange;

            CurRangeData = explodeData;

#if UNITY_EDITOR
            debugRangeData = explodeData;
#endif
        }

        protected async override void FireAsync(ICanHitObj target)
        {
            if (target == null) return;
            await BulletFactory.CreateBulletAsync<Bullet_Bomb>(GetBulletData(target));
        }

        protected override ST_BulletData GetBulletData(ICanHitObj target)
        {
            var tmp= base.GetBulletData(target);
            tmp.rangeData = CurRangeData;
            return tmp;
        }

        public override string GetDes()
        {
            base.GetDes();
            SB.Append("\n范围:");
            SB.Append(CurRangeData.dRadius.ToString("0.0"));
            return SB.ToString();
        }

#if UNITY_EDITOR
        public static STMyComponetData GetMyComponetData(ST_GunData gunData,ST_RangeData explodeData)
        {
            STMyComponetData componentData = GetMyComponetData(gunData);
            componentData.strType = typeof(Cannon).FullName;
            componentData.data[Key_RangeData]= JsonMapper.ToObject(JsonMapper.ToJson(explodeData));
            
            return componentData;
        }
#endif
    }
}